Page({

   /**
    * 页面的初始数据
    */
   data: {
      value: '',
      repos: {},
      items: [{
            type: 'radio',
            label: '类型',
            value: 'type',
            children: [{
                  label: '综合',
                  value: '01',
               },
               {
                  label: '理工',
                  value: '02',
               },
               {
                  label: '财经',
                  value: '03',
               },
               {
                  label: '农林',
                  value: '04',
               },
            ],
            groups: ['001'],
         },
         {
            type: 'radio',
            label: '批次',
            value: 'size',
            children: [{
                  label: '本科一批',
                  value: '01',
               },
               {
                  label: '本科二批',
                  value: '02',
               },
               {
                  label: '本科提前A',
                  value: '03',
               },
               {
                  label: '本科提前B',
                  value: '04',
               },
               {
                  label: '本科一批B',
                  value: '05',
               },
               {
                  label: '本科二批B',
                  value: '06',
               },
               {
                  label: '专科提前',
                  value: '07',
               },
               {
                  label: '高职高专',
                  value: '08',
               },
            ],
            groups: ['002'],
         },
         {
            type: 'sort',
            label: '排名',
            value: 'order',
            groups: ['003'],
         },
         {
            type: 'filter',
            label: '筛选',
            value: 'filter',
            children: [{
                  type: 'radio',
                  label: 'Languages（单选）',
                  value: 'language',
                  children: [{
                        label: 'JavaScript',
                        value: 'javascript',
                     },
                     {
                        label: 'HTML',
                        value: 'html',
                     },
                     {
                        label: 'CSS',
                        value: 'css',
                     },
                     {
                        label: 'TypeScript',
                        value: 'typescript',
                     },
                  ],
               },
               {
                  type: 'checkbox',
                  label: 'Query（复选）',
                  value: 'query',
                  children: [{
                        label: 'Angular',
                        value: 'angular',
                     },
                     {
                        label: 'Vue',
                        value: 'vue',
                     },
                     {
                        label: 'React',
                        value: 'react',
                     },
                     {
                        label: 'Avalon',
                        value: 'avalon',
                     },
                  ],
               }
            ],
            groups: ['001', '002', '003'],
         },
      ],
   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function(options) {
      // this.getRepos()
   },

   onBlur(e) {
      console.log('onBlur', e)
      wx.navigateTo({
         url: '/pages/index/index',
      })
   },
   onConfirm(e) {
      console.log('onConfirm', e)
   },
   onClear(e) {
      console.log('onClear', e)
      this.setData({
         value: '',
      })
   },
   onCancel(e) {
      console.log('onCancel', e)
      wx.navigateTo({
         url: '/pages/index/index',
      })
   },
   // filterBar
   onChange(e) {
      const {
         checkedItems,
         items
      } = e.detail
      const params = {}

      console.log(checkedItems, items)

      checkedItems.forEach((n) => {
         if (n.checked) {
            if (n.value === 'updated') {
               const selected = n.children.filter((n) => n.checked).map((n) => n.value).join(' ')
               params.sort = n.value
               params.order = selected
            } else if (n.value === 'stars') {
               params.sort = n.value
               params.order = n.sort === 1 ? 'asc' : 'desc'
            } else if (n.value === 'forks') {
               params.sort = n.value
            } else if (n.value === 'filter') {
               n.children.filter((n) => n.selected).forEach((n) => {
                  if (n.value === 'language') {
                     const selected = n.children.filter((n) => n.checked).map((n) => n.value).join(' ')
                     params.language = selected
                  } else if (n.value === 'query') {
                     const selected = n.children.filter((n) => n.checked).map((n) => n.value).join(' ')
                     params.query = selected
                  }
               })
            }
         }
      })

      this.getRepos(params)
   },
   getRepos(params = {}) {
      const language = params.language || 'javascript'
      const query = params.query || 'react'
      const q = `${query}+language:${language}`
      const data = Object.assign({
         q,
      }, params)

      wx.showLoading()
      wx.request({
         url: `https://api.github.com/search/repositories`,
         data,
         success: (res) => {
            console.log(res.data)

            wx.hideLoading()

            this.setData({
               repos: res.data.items.map((n) => Object.assign({}, n, {
                  date: n.created_at.substr(0, 7),
               })),
            })
         },
      })
   },
   onOpen(e) {
      this.setData({
         pageStyle: 'height: 100%; overflow: hidden',
      })
   },
   onClose(e) {
      this.setData({
         pageStyle: '',
      })
   },
   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function() {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function() {

   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function() {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function() {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function() {

   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function() {

   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function() {

   }
})